package com.freecloud.bitorrent.domain.server.support;

import cn.hutool.core.util.ObjectUtil;
import com.freecloud.bitorrent.common.exceptions.NotFoundException;
import com.freecloud.bitorrent.enums.QueryEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 * @Author: maomao
 * @Date: 2022/7/23 5:08 PM
 */
@Slf4j
@Component
public class DhtCommandFactory {

    private final Map<String, DhtCommand> dhtCommandMap;

    public DhtCommandFactory(Map<String, DhtCommand> dhtCommandMap) {
        this.dhtCommandMap = dhtCommandMap;
    }

    public DhtCommand getDhtCommand(QueryEnum queryEnum) {
        if (dhtCommandMap.containsKey(queryEnum.getValue())) {
            return dhtCommandMap.get(queryEnum.getValue());
        }
        throw new NotFoundException(String.format("[%s] DhtCommand not found ！", queryEnum));
    }

    public DhtEncode getDhtCommandEncode(QueryEnum queryEnum) {
        DhtCommand dhtCommand = dhtCommandMap.get(queryEnum.getValue());
        if (ObjectUtil.isNotNull(dhtCommand) && dhtCommand instanceof DhtEncode) {
            return (DhtEncode)dhtCommand;
        }
        throw new NotFoundException(String.format("[%s] DhtEncode not found ！", queryEnum));
    }
}
